package com.wantup.demo01;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Zhaoluowei
 * @version 1.0
 * @date 2019/6/26 16:45
 * <p>
 * 控制线程按序执行
 */
public class TestABCAlternate {

    public static void main(String[] args) {
        AlternateDemo alternateDemo = new AlternateDemo();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= 20; i++) {
                    alternateDemo.loopA(i);
                }
            }
        }, "A").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= 20; i++) {
                    alternateDemo.loopB(i);
                }
            }
        }, "B").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= 20; i++) {
                    alternateDemo.loopC(i);

                    System.out.println("---------------------------------------");
                }
            }
        }, "C").start();

    }

}

class AlternateDemo {
    private int number = 1;//当前正在执行线程的标记
    private Lock lock = new ReentrantLock();
    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();

    /**
     * @param totalLoop 循环几轮
     */
    public void loopA(int totalLoop) {
        try {
            lock.lock();

            //1.判断
            if (number != 1) {
                condition1.await();
            }

            //2. 打印
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + " \t " + i + " \t " + totalLoop);
            }

            //唤醒其他线程
            number = 2;
            condition2.signal();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }


    public void loopB(int totalLoop) {
        try {
            lock.lock();

            //1.判断
            if (number != 2) {
                condition2.await();
            }

            //2. 打印
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + " \t " + i + " \t " + totalLoop);
            }

            //唤醒其他线程
            number = 3;
            condition3.signal();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }


    public void loopC(int totalLoop) {
        try {
            lock.lock();

            //1.判断
            if (number != 3) {
                condition3.await();
            }

            //2. 打印
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + " \t " + i + " \t " + totalLoop);
            }

            //唤醒其他线程
            number = 1;
            condition1.signal();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }


}


